package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //创建一个shopping对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //使用复制方法
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        //确定当前是本人在使用
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //根据shopping对象 返回一个集合
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //判断  如果list ！=null 并且 list.size 大于0时
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //获取当前唯一对象
            shoppingCart = shoppingCartList.get(0);
            //购物车对象数量加一
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            //修改
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            //获取dishId
            Long dishId = shoppingCartDTO.getDishId();
            //判断如果dishid不为null时 说明传进来的是dishId
            if (dishId != null) {
                //通过id把对象返回 然后在上传dish的信息
                Dish dish = dishMapper.findID(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());


            } else {
                Setmeal setmeal = setmealMapper.findById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {

        List<ShoppingCart> list = shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
        return list;
    }

    @Override
    public void deleteAll() {
        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
         shoppingCart = list.get(0);

        if(shoppingCart.getNumber()==1){
            if (shoppingCart.getDishId()!=null){
                shoppingCartMapper.deleteOne(shoppingCart);
            }else {
                shoppingCartMapper.deleteOne(shoppingCart);
            }
            return;
        }
        shoppingCartMapper.updateById(shoppingCart);

        }
    }
